package boy.contrib.display
{
   import flash.display.Bitmap;
   import flash.display.BitmapData;
   import flash.display.BitmapDataChannel;
   import flash.display.BlendMode;
   import flash.display.DisplayObjectContainer;
   import flash.display.GradientType;
   import flash.display.Sprite;
   import flash.geom.Matrix;
   import flash.geom.Point;

   /**
    * Create a shadow sphere. It can be a source of DisplacementMapFilter for Fish-Eye effect.
    * Still can't understand what is this? see --> ShadowSphere.png
    *    - TODO : support reverse mode.
    * @author     cjboy | cjboy1984@gmail.com
    * @usage
    * var sp:ShadowSphere = new ShadowSphere(70);
    * addChild(sp);
    *
    * or
    *
    * var sp:ShadowSphere = new ShadowSphere(70);
    * trace(sp.bitmapData);
    */
   public class ShadowSphere extends Sprite
   {
      private const BG_COLOR:uint = 0x808080;
      private const DEF_RADIUS:Number = 100;

      private var _radius:Number;

      private var cover:Sprite;
      private var green:Sprite;
      private var red:Sprite;
      private var bg:Sprite;

      /**
       * Constructor of shadow sphere.
       * @param	radius
       */
      public function ShadowSphere(radius:Number, reverse:Boolean = false)
      {
         _radius = radius;

         // background
         bg = new Sprite();
         bg.graphics.beginFill(BG_COLOR, 1);
         bg.graphics.drawRect(-DEF_RADIUS, -DEF_RADIUS, DEF_RADIUS * 2, DEF_RADIUS * 2);
         bg.graphics.endFill();
         addChild(bg);

         // red
         red = new Sprite();
         red.graphics.beginGradientFill(GradientType.LINEAR, [0xff0000, 0x000000], [1, 1], [0, 255]);
         red.graphics.drawCircle(0, 0, DEF_RADIUS);
         red.graphics.endFill();
         addChild(red);

         // green
         var green_mx:Matrix = new Matrix();
         green_mx.createGradientBox(DEF_RADIUS * 2, DEF_RADIUS * 2, Math.PI / 2, -DEF_RADIUS, -DEF_RADIUS);
         green = new Sprite();
         green.graphics.beginGradientFill(GradientType.LINEAR, [0x00ff00, 0x000000], [1, 1], [0, 255], green_mx);
         green.graphics.drawCircle(0, 0, DEF_RADIUS);
         green.graphics.endFill();
         green.blendMode = BlendMode.ADD;
         addChild(green);

         // cover
         cover = new Sprite();
         cover.graphics.beginGradientFill(GradientType.RADIAL, [0x808080, 0x808080, 0x808080], [0, 0.52, 1], [56, 209, 255]);
         cover.graphics.drawCircle(0, 0, DEF_RADIUS);
         cover.graphics.endFill();
         addChild(cover);

         // change size
         bg.width = bg.height = radius;
         red.width = red.height = radius;
         green.width = green.height = radius;
         cover.width = cover.height = radius;
      }

      // ------------------ LINE -------------------

      public function get bitmapData():BitmapData
      {
         var bmpData:BitmapData = new BitmapData(width, height, false, BG_COLOR);
         bmpData.draw(this, new Matrix(scaleX, 0, 0, scaleY, width / 2, height / 2));

         return bmpData;
      }

      // ################ Protected ################

      // ################# Private #################

      // ------------------ LINE -------------------

   }

}